// 计算方向

import '../const/type.dart';

Direction getDirection(RenderItem renderItem, double rotate) {
  int len = renderItem.txtList.length;
  double offsetRadius = renderItem.offset * 360 / len;
  var orientationItem =
      findIt(list: renderItem.txtList, rotate: rotate, offset: offsetRadius);
  var seat = findIt(
      list: renderItem.txtList, rotate: rotate, offset: 180 + offsetRadius);
  return Direction(seat: seat, orientation: orientationItem);
}

CircleTxt? findIt(
    {required List<CircleTxt> list,
    required double rotate,
    double totalAngle = 360,
    double offset = 0}) {
  if (list.isEmpty) {
    return null;
  }
  var angle = totalAngle / list.length;
  var index = ((rotate - offset + angle / 2) % 360 / angle).floor();

  if (index < 0) {
    index = list.length - 1;
  } else if (index >= list.length) {
    index = 0;
  }

  var item = list[index];
  if (item.stopCalcDirection || item.children == null) {
    return item;
  }
  return findIt(
      list: item.children!,
      rotate: rotate,
      totalAngle: angle,
      offset: offset + index * angle);
}

String? findFiveLineAttribute(CircleTxt? circleTxt) {
  if (circleTxt == null) {
    return null;
  }
  return circleTxt.infoList?.firstWhere(
      (element) => element['fiveLineAttribute'] != null)['fiveLineAttribute'];
}

String? findYinYang(CircleTxt? circleTxt) {
  if (circleTxt == null) {
    return null;
  }
  return circleTxt.infoList
      ?.firstWhere((element) => element['yinYang'] != null)['yinYang'];
}
